package com.epower.web.action.epowerwordforday;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.epower.base.BaseAction;
import com.epower.bo.epower.commons.Result;
import com.epower.bo.epowerbook.EpowerBookService;
import com.epower.bo.epowersentence.EpowerSentenceService;
import com.epower.bo.epoweruser.EpowerUserService;
import com.epower.bo.epowerwordforday.EpowerWordForDayService;
import com.epower.bo.epowerwordforday.impl.EpowerWordForDayServiceImpl.WordForDayResult;
import com.epower.dataobject.EpowerBookDO;
import com.epower.dataobject.EpowerUserDO;
import com.epower.web.action.utils.TemplateUtils;

@Scope("prototype")
@Component()
public class EpowerWordForDayAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private String date;
	private EpowerWordForDayService epowerWordForDayBO;
	private EpowerSentenceService epowerSentenceBO;

	private EpowerUserService epowerUserBO;

	private EpowerBookService epowerBookBO;
	private Result result = Result.succResult();

	/**
	 * http://localhost:8080/epower-site-webapp-1.0.0/jsonepowermainword/
	 * getEpowerWordForDay.action?date=2012-4-4
	 * 
	 * @return
	 * @throws Exception
	 */
	public String getEpowerWordForDay() throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if (date == null || "".equals(date)) {
			result.setResultCode(Result.RESULTCODE_PARAMETER_ILLEGAL);
			resultMap.put("message", "date不能为空");
			return ERROR;
		}

		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		WordForDayResult wordResult = epowerWordForDayBO.getEpowerWordForDayByDate(format.parse(date));
		Map<String, Object> model = new HashMap<String, Object>();
		if(wordResult.getMain()!=null && wordResult.getList() !=null){
			model.put("mainWord", wordResult.getMain());
			model.put("cognateWords", wordResult.getList());
			String tempPath = ServletActionContext.getServletContext().getRealPath("/") + "WEB-INF/freemarker";// 模板路劲
			String centerContet = TemplateUtils.html2String(model, "wordforday.ftl", tempPath);// 加载模板
			resultMap.put("centerContent", centerContet);
		}
		result.setResultCode(Result.RESULTCODE_NORMAL);
		result.setResult(resultMap);
		return SUCCESS;
	}

	public String viewReadEpowerWordForDay() throws Exception {
		return SUCCESS;
	}

	public String getUserCurrentInf() {
		EpowerUserDO user = this.getUser();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if (user != null && user.getCurrentbook() != null) {
			EpowerBookDO currentBook = epowerBookBO.getEpowerBookById(user.getCurrentbook());
			if (currentBook != null) {
				resultMap.put("bookName", currentBook.getLibname());
				resultMap.put("quantity", currentBook.getQuantity());
				resultMap.put("percentage", currentBook.getPercentage(user.getId()));
				resultMap.put("lastTime", user.getUpdatetime());
			}

		}
		result.setResultCode(Result.RESULTCODE_NORMAL);
		result.setResult(resultMap);
		return SUCCESS;
	}

	public String viewEpowerWordForDay() throws Exception {
		return SUCCESS;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public EpowerWordForDayService getEpowerWordForDayBO() {
		return epowerWordForDayBO;
	}

	public void setEpowerWordForDayBO(EpowerWordForDayService epowerWordForDayBO) {
		this.epowerWordForDayBO = epowerWordForDayBO;
	}

	public EpowerSentenceService getEpowerSentenceBO() {
		return epowerSentenceBO;
	}

	public void setEpowerSentenceBO(EpowerSentenceService epowerSentenceBO) {
		this.epowerSentenceBO = epowerSentenceBO;
	}

	public Result getResult() {
		return result;
	}

	public void setResult(Result result) {
		this.result = result;
	}

	public void setEpowerUserBO(EpowerUserService epowerUserBO) {
		this.epowerUserBO = epowerUserBO;
	}

	public void setEpowerBookBO(EpowerBookService epowerBookBO) {
		this.epowerBookBO = epowerBookBO;
	}

}
